package com.qk.management.controller;


import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.UserDO;
import com.qk.dto.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }




    @DeleteMapping("/{ids}")
    public Result deleteBatch(@PathVariable List<Integer> ids){
        userService.deleteBatch(ids);
        return Result.success();
    }

    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("新增用户: {}", user);
        userService.addUser(user);
        return Result.success();
    }


    @GetMapping
    public Result getUsers(UserDTO userDTO) {
        PageResult<UserDO> userPage = userService.getUsers(userDTO);
        return Result.success(userPage);
    }
}
